//index.js
//获取应用实例
const app = getApp();
import HttpRequst from '../../../utils/httpRequst.js'
import API from '../../../utils/config.js'
import SendFormId from '../../../utils/sendFormId.js'




Page({
  data: {
    fwzp: "",
    fwzpStatus: "",
    isLoading: false,
    isDataEnd: false,
    isNoData: false,
    cs_tel: "",
    cates:[],
   
    page: 1,
    isTabFixed: false,
    tabOffsetTop: "",
    systermInfo:"",
    loan: "",
    account: "",
    stepd: "",
    pagesize: 20,
    listType:"",
    isFold: true, //查完完整流程
    lists: []
  },
  goToSeeContract: function() {
    var that = this;
    wx.showLoading();
    wx.downloadFile({
      url: that.data.loan.list[2].contract,
      success: function(res) {
        wx.hideLoading();

        var filePath = res.tempFilePath
        wx.openDocument({
          filePath: filePath,
          success: function(res) {

          }
        })
      }
    })
  },


  onLoad: function() {
   
    var that = this;
    HttpRequst(false, API.home, 1, app.getUkey(), {
      "deviceName": app.globalData.deviceName,
      "version": app.globalData.version
    }, "POST", false, function(res) {
      that.setData({
        cs_tel: res.data.cs_tel,
        cates: res.data.cates,
        
        stepd: res.data.stepd,
        isFold: res.data.stepd == 5 ? false : true,
        account: res.data.account,
        loan: res.data.loan || ""
      });
    });

    HttpRequst(false, API.bizs, 1, app.getUkey(), "", "POST", false, function(res) {
      
      that.setData({
        lists: res.data.bizs,
        isNoData: !res.data.bizs.length ? true : false,
        page: that.data.page + 1,
        isDataEnd: (res.data.bizs.length < that.data.pagesize) ? true : false
      });
    });

    if (app.getUkey()) {
      HttpRequst(false, API.houseimgs, 1, app.getUkey(), "", "POST", false, function(res) {
        that.setData({
          fwzp: res.data,
          fwzpStatus: that.getFwzpStatus(res.data)
        });
      });
    }

    that.setData({
      systermInfo: app.globalData.systemInfo
    });
    
  },
  formForIdSubmit1:function(e){
    app.getFormIds(e.detail.formId);
    wx.navigateTo({
      url: '/pages/index/login/login'
    })
  },
  //点击在线签名
  formForIdSubmit2: function (e) {
    app.getFormIds(e.detail.formId);
    SendFormId(app, "POST", 2, "");
    wx.navigateTo({
      url: '/pages/index/signonline/signonline'
    })
  },
  //点击查看合同
  formForIdSubmit3: function (e) {
    
    app.getFormIds(e.detail.formId);
    SendFormId(app, "POST", 2, "");
    wx.navigateTo({
      url: '/pages/index/mineht/mineht'
    })
  },
  //点击上传房屋照片
  formForIdSubmit4:function(e){
    app.getFormIds(e.detail.formId);
    SendFormId(app, "POST", 2, "");
    wx.navigateTo({
      url: '/pages/index/uploadfwzp/uploadfwzp'
    })
  },

  formForIdSubmit5:function(e){
    app.getFormIds(e.detail.formId);
    SendFormId(app, "POST", 2, "");
    wx.navigateTo({
      url: '/pages/index/mineindex/mineindex?money=' + this.data.account.money + '&use_money=' + this.data.account.use_money + '&left_money=' + this.data.account.left_money
    })
  },
  
  getFwzpStatus: function(data) {
    // f_stauts -1 表示审核失败   0 未审核    2 审核中  1审核通过
    //
    //1审核通过
    //审核中  2
    //审核失败  -1
    //审核通过 3
    var flag;
    if (!data.sfz && !data.zxq && !data.zxh){
      flag=false;
    }else{
      if (data.sfz.status == 1 && data.zxq.status == 1 && data.zxh.status == 1) {
        flag = "allcross";
      } else {
        if (data.zxq.status == 0 || data.zxq.status == 2) {
          flag = "djjr";
        } else if (data.zxq.status == -1 || data.sfz.status == -1 || data.zxh.status == -1) {
          flag = "shsb";
        } else if (data.zxq.status == 2 || data.sfz.status == 2 || data.zxh.status == 2){
          flag = "shz";
        }else{
          flag=false;
        }
      }
    }
    return flag;
  },

  onShow: function() {
    if (this.data.tabOffsetTop) return;
    var that = this;

    setTimeout(function() {
      var query = wx.createSelectorQuery()
      query.select('#J_tab-hd').boundingClientRect()
      query.exec(function(res) {
        that.setData({
          tabOffsetTop: res[0].top
        })
      })
    }, 500)

  },
  onReady: function () {
   
    var that = this;

    setTimeout(function () {
      var query = wx.createSelectorQuery()
      query.select('#J_tab-hd').boundingClientRect()
      query.exec(function (res) {
        that.setData({
          tabOffsetTop: res[0].top
        })
      })
    },500)

  },
  imgload: function() {
    var that = this;
    var query = wx.createSelectorQuery();
    query.select('#J_tab-hd').boundingClientRect()
    query.exec(function(res) {
      that.setData({
        tabOffsetTop: res[0].top
      })
    })
  },
  onPageScroll: function(e) {
    
  
    if (e.scrollTop >= this.data.tabOffsetTop) {
      this.setData({
        isTabFixed: true
      });
    } else {
      this.setData({
        isTabFixed: false
      });
    }
  },



  seeAllStep: function() {
    var that = this;
    this.setData({
      isFold: !this.data.isFold
    });

    var query = wx.createSelectorQuery()
    query.select('#J_section-loan-step').boundingClientRect()
    query.exec(function(res) {

      that.setData({
        tabOffsetTop: res[0].height + 15
      })
    })

  },


  makePhoneCall: function(e) {
    wx.makePhoneCall({
      phoneNumber: this.data.cs_tel,
    })
  },

  makePhoneCallKF: function(e) {
    wx.makePhoneCall({
      phoneNumber: "19959277915",
    })
  },






  navShopHandle: function(e) {
    var that = this;
    //点击当前的选项 直接返回
    if (e.currentTarget.dataset.type == this.data.listType) return;
   
    this.setData({
      page: 1, //重新设置页数为1
      lists: [], //清空数据
      isDataEnd: false,
      isLoading: true,
      isNoData: false,
      listType: e.currentTarget.dataset.type
    });
    HttpRequst(false, API.bizs, 1, "", {
      cate2_id: this.data.listType,
      pagesize: this.data.pagesize
    }, "POST", false, function(res) {
      that.setData({
        isLoading: false
      })
      if (!!res.status) {
        that.setData({
          lists: res.data.bizs,
          isNoData: !res.data.bizs.length ? true : false,
          page: that.data.page + 1,
          isDataEnd: (res.data.bizs.length < that.data.len) ? true : false
        });
      } else {
        wx.showToast({
          title: '服务器繁忙，请重试',
          icon: 'none',
          duration: 5000
        });
      }
    })
  },





  onReachBottom: function() {
    if (this.data.isDataEnd) return;

    var that = this;
    this.setData({
      isLoading: true
    });
    HttpRequst(false, API.bizs, 1, "", {
      cate2_id: this.data.listType,
      page: this.data.page,
      pagesize: this.data.pagesize
    }, "POST", false, function(res) {
      that.setData({
        isLoading: false
      });
      if (!!res.status) {

        that.setData({
          lists: that.data.lists.concat(res.data.bizs),
          page: that.data.page + 1,
          isDataEnd: (res.data.bizs.length < that.data.pagesize) ? true : false
        });
      } else {
        wx.showToast({
          title: '服务器繁忙，请重试',
          icon: 'none',
          duration: 5000
        });
      }

    })

  },

  onPullDownRefresh: function() {
    var that = this;
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.setData({
      lists: [],
      listType:"",
      page: 1
    })

    HttpRequst(false, API.home, 1, app.getUkey(), {
      cate2_id: this.data.listType,
      page: this.data.page,

      pagesize: this.data.pagesize
    }, "POST", false, function(res) {


      that.setData({
        cs_tel: res.data.cs_tel,
       
        stepd: res.data.stepd,
        account: res.data.account,
        isFold: res.data.stepd == 5 ? false : true,
        loan: res.data.loan || ""
      });
    });

    HttpRequst(false, API.bizs, 1, app.getUkey(), "", "POST", false, function(res) {
      wx.hideNavigationBarLoading()
      wx.stopPullDownRefresh()
      that.setData({
        lists: res.data.bizs,
        page: that.data.page + 1,
        isNoData: !res.data.bizs.length ? true : false,
        isDataEnd: (res.data.bizs.length < that.data.pagesize) ? true : false
      });
    });


    if (app.getUkey()) {
      HttpRequst(false, API.houseimgs, 1, app.getUkey(), "", "POST", false, function(res) {
        that.setData({
          fwzp: res.data,
          fwzpStatus: that.getFwzpStatus(res.data)
        });
      });
    }





  },
  logout: function() {
    app.logout();
  },
  onShareAppMessage: function() {
    return {
      title: '「家+贷」农行&小鱼联合打造',
      path: '/pages/index/index/index',
      imageUrl: "https://api.haodian.cn/img/wechatapp/jjd/jiajiadai_share4.jpg",
      success: function(res) {
        wx.showToast({
          title: '成功',
          icon: 'success',
          duration: 2000
        })
      },
      fail: function(res) {

      }
    }
  }
})